package com.shijiu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shijiu.common.Result;
import com.shijiu.dao.mapper.UserMapper;
import com.shijiu.pojo.User;
import com.shijiu.service.ShopService;
import com.shijiu.service.UserService;
import com.shijiu.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author xlj
 * @Date Created in  2023/3/18 14:30
 * @DESCRIPTION:
 * @Version V1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopService shopService;

    /**
     *  根据账号密码查询用户
     * @param username 登录账号
     * @param password 登录密码
     * @return 返回数据库查询出来的 User
     */
    @Override
    public User loginFindUser(String username, String password) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username)
                .eq(User::getPassword,password)
                .select(User::getId,User::getUsername,User::getPassword,User::getIsAdmin)
                .last("limit 1 ");
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Result queryUserList(PageParams pageParams) {
        Integer pageCount = pageParams.getPage();
        Integer pageSize = pageParams.getPageSize();
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        userMapper.selectList(lambdaQueryWrapper);
        // 每页查询pageSize条记录
        //lambdaQueryWrapper.last(String.valueOf(pageSize));
        // 查询第pageCount页，每页pageSize条记录
        Page<User> page = new Page<>(pageCount, pageSize);
        IPage<User> userPage = userMapper.selectPage(page, lambdaQueryWrapper);
        // 当前页的记录列表
        List<User> userList = userPage.getRecords();
        // 总记录数
        long total = userPage.getTotal();
        UserListVo userListVo = new UserListVo();
        userListVo.setUserVoList(userList);
        userListVo.setTotal(total);
        return Result.success(userListVo);
    }

}
